<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${table.basePackage}.mapper.${table.entityName}Mapper">
    <!-- ${table.comment!table.entityName} | table: ${table.tableName} | template: single -->

    <resultMap id="BaseResultMap" type="${table.basePackage}.entity.${table.entityName}">
        <#-- 先输出所有主键字段（<id>元素必须在<result>元素之前） -->
        <#list columns as c>
        <#if c.isPk?? && c.isPk>
        <id property="${(c.propertyName!'id')}" column="${(c.columnName!'id')}" />
        </#if>
        </#list>
        <#-- 再输出所有非主键字段 -->
        <#list columns as c>
        <#if !(c.isPk?? && c.isPk)>
        <result property="${(c.propertyName!'id')}" column="${(c.columnName!'id')}" />
        </#if>
        </#list>
    </resultMap>

    <sql id="Base_Column_List">
        <#list columns as c><#if c.columnName??>${c.columnName}<#else>id</#if><#if c_has_next>, </#if></#list>
    </sql>

    <!-- 基础查询示例，可根据需要扩展 -->
    <select id="selectByFilter" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from ${table.tableName}
        <where>
        <#-- 按照uiOrder字段排序，如果没有则按原来的顺序 -->
        <#assign sortedColumns = columns?sort_by("uiOrder")>
        <#list sortedColumns as c>
          <#-- 跳过BaseEntity中已有的字段，但允许查询条件 -->
          <#assign skipField = false>
          <#if c.columnName?? && (c.columnName?lower_case == "create_by" || c.columnName?lower_case == "update_by")>
            <#assign skipField = true>
          </#if>
          
          <#if c.columnName?? && c.propertyName?? && (c.isQueryCond?? && c.isQueryCond) && !skipField>
            <#if (c.javaType?? && c.javaType?contains('String'))>
              <if test="filter.${c.propertyName} != null and filter.${c.propertyName} != ''">
                and ${c.columnName} like concat('%', #{filter.${c.propertyName}}, '%')
              </if>
            <#else>
              <if test="filter.${c.propertyName} != null">
                and ${c.columnName} = #{filter.${c.propertyName}}
              </if>
            </#if>
          </#if>
        </#list>
        </where>
        order by id desc
    </select>
</mapper>


